<?php

class Upload extends CI_Controller {
    
    private $url;

    function __construct(){
        parent::__construct();
        $this->url = "http://localhost/proyecto/proyecto/";
        if(!$this->session->userdata('nombre')){
           $data = array(
               "redirect" => "Upload",
               "error" => "Usuario no autorizado"
           );
           $this->twig->display("login.html",$data);
           die();
        }
    }

    function index(){
        $this->twig->display("jquery.html");
    }

    function do_upload($seccion,$galeria,$idSeccion,$idGaleria){
        $dir = $seccion."/".$galeria;
        @mkdir("./fotos/".$dir."/mini", 0777,TRUE);
        $config['upload_path'] = './fotos/'.$dir."/";
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']	= '1024';
        $config['max_width']  = '1920';
        $config['max_height']  = '1080';
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload()){
            $error = array('error' => $this->upload->display_errors());
            print_r($error);
        }
        else{
            $data = array('upload_data' => $this->upload->data());
            $this->load->model('modelImage');
            $this->load->model('fotos');
            $nombre = $data['upload_data']['file_name'];
            $this->modelImage->redimencionar($seccion,$galeria,$nombre);
            $info['-'] = $config['upload_path'];
            $info['url'] = $this->url."fotos/".$dir."/".$nombre;
            $info['name'] = $data['upload_data']['file_name'];
            $info['type'] = $data['upload_data']['file_type'];
            $info['size'] = $data['upload_data']['file_size'];
            $info['thumbnail_url'] = $this->url."fotos/".$dir."/mini/".$nombre;
            $info['delete_url'] = $this->url."borrar";
            $info['delete_type'] = "DELETE";
            $this->fotos->agregarImagen($idSeccion,$idGaleria,$nombre,$dir);
            echo json_encode(array($info));
        }
    }
}
?>
